/*
 * @lc app=leetcode.cn id=478 lang=cpp
 *
 * [478] 在圆内随机生成点
 */
#include "include.h"
// @lc code=start
class Solution {
private:
    mt19937 gen{random_device{}()};
    uniform_real_distribution<double> dis;
    double xc, yc, r;

public:
    Solution(double radius, double x_center, double y_center): dis(-radius, radius), xc(x_center), yc(y_center), r(radius) {}
    
    vector<double> randPoint() {
        while (true) {
            double x = dis(gen), y = dis(gen);
            if (x * x + y * y <= r * r) {
                return {xc + x, yc + y};
            }
        }
    }
};

/**
 * Your Solution object will be instantiated and called as such:
 * Solution* obj = new Solution(radius, x_center, y_center);
 * vector<double> param_1 = obj->randPoint();
 */
// @lc code=end

class Solution {
public:
    Solution(double radius, double x_center, double y_center) {
        radius_ = radius;
        x_center_ = x_center;
        y_center_ = y_center;
    }
    
    vector<double> randPoint() {
        double r, theta;
        int X, Y;
        double x, y;

    resample:
        srand((unsigned)time(NULL));
        X = rand() % modNum_;
        srand((unsigned)time(NULL));
        Y = rand() % modNum_;

        x = 2 * radius_ * X / modNum_;
        y = 2 * radius_ * Y / modNum_;

        bool inside = pow(x - radius_, 2) + pow(x - radius_, 2) < radius_ * radius_;
        if (!inside){
            goto resample; 
        }

        vector<double> point;
        point.emplace_back(x_center_ - radius_ + x);
        point.emplace_back(y_center_ - radius_ + y);
        return point;
    }

    // vector<double> randPoint() {
    //     double r, theta;
    //     int R, Theta;
    //     srand((unsigned)time(NULL));
    //     // RAND_MAX;
    //     R = rand() % modNum_;
    //     srand((unsigned)time(NULL));
    //     Theta = rand() % modNum_;

    //     r = R * radius_ * radius_ / modNum_;
    //     theta = Theta * 2 * M_PI / modNum_;

    //     double cosTheta = cos(theta);
    //     double sinTheta = sin(theta);

    //     vector<double> point;
    //     point.emplace_back(x_center_ + cosTheta * radius_);
    //     point.emplace_back(y_center_ + sinTheta * radius_);
    //     return point;
    // }
private:
    double radius_;
    double x_center_;
    double y_center_;

    const int modNum_ = RAND_MAX;
};
